<?php
/**
 * Created by PhpStorm.
 * User: wanghaibo
 * Date: 18/3/26
 * Time: 16:22
 */

namespace App\Http\Api\Admin;


use App\Http\API\ApiController;
use App\Services\Admin\UserService;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class AuthApiController extends ApiController
{
    use AuthenticatesUsers;

    /**
     * @param Request $request
     * @return array
     */
    protected function validator(Request $request){
        return $this->validate(
            $request,
            [
                'password' => 'required|min:6|confirmed',
                'email'     => 'required|unique:users',
                'token' => 'required',
            ]
        );
    }
    public function login(Request $request, UserService $service){
        if($this->attemptLogin($request)){
            return $this->success(1,
                [
                    'token'=>$service->issueJWTToken(Auth::id()),
                    'user'  =>  Auth::user()
                ],
                '登陆成功');
        }
        return $this->fail(2,[],'密码错误');
    }
}